<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Fenxiao_Config extends Stourweb_Controller
{
    public function before()
    {
        parent::before();
    }

    //参数开关
    public function action_params()
    {
        $list=ORM::factory('fenxiao_config')->get_all();
        $info=array();
        foreach($list as $v)
        {
            $info[$v['varname']]=$v['value'];
        }
        $this->assign('info',$info);
        $this->display('admin/fenxiao/config/params');
    }

    //微信设置
    public function action_weixin()
    {
        $list=ORM::factory('fenxiao_config')->get_all();
        $info=array();
        foreach($list as $v)
        {
            $info[$v['varname']]=$v['value'];
        }
        $this->assign('info',$info);
        $this->display('admin/fenxiao/config/weixin');
    }

    //介绍
    public function action_jieshao()
    {
        $list=ORM::factory('fenxiao_config')->get_all();
        $info=array();
        foreach($list as $v)
        {
            $info[$v['varname']]=$v['value'];
        }
        $this->assign('info',$info);
        $this->display('admin/fenxiao/config/jieshao');
    }

    //分销方式
    public function action_way()
    {


        $cfg_m_logo = Model_Sysconfig::get_configs(0,'cfg_m_logo',true);
        $cfg_fenxiao_df_fx_shop_logo = Model_Fenxiao_Config::get_val('cfg_fenxiao_df_fx_shop_logo');
        $list=ORM::factory('fenxiao_config')->get_all();
        $info=array();
        foreach($list as $v)
        {
            $info[$v['varname']]=$v['value'];
        }

        $ways_arr = explode(',', $info['cfg_fenxiao_ways']);
        $pic_parts_arr = explode(',',$info['cfg_fenxiao_pic_parts']);

        $this->assign('info',$info);
        $this->assign('ways_arr',$ways_arr);
        $this->assign('pic_parts_arr',$pic_parts_arr);
        $this->assign('cfg_m_logo',$cfg_m_logo);
        $this->assign('cfg_fenxiao_df_fx_shop_logo',$cfg_fenxiao_df_fx_shop_logo);
        $this->display('admin/fenxiao/config/way');
    }

    //保存配置
    public function action_ajax_config_save()
    {
        $olg_fenxiao_status_batch = DB::select()->from('fenxiao_config')->where("varname='cfg_main_fenxiao_status_batch'")->execute()->get('value');
        foreach($_POST as $k=>$v)
        {
            if(strpos($k,'cfg_')!==false)
            {
                $model=ORM::factory('fenxiao_config')->where('varname','=',$k)->find();
                $model->varname=$k;
                $model->value=$v;
                $model->groupid=0;
                $model->save();
            }
        }
        $fenxiao_status_batch = $_POST['cfg_main_fenxiao_status_batch'];
        if($olg_fenxiao_status_batch!=$fenxiao_status_batch){
            if(St_Functions::is_normal_app_install('system_line')){
                DB::update('line')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_hotel')){
                DB::update('hotel')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_car')){
                DB::update('car')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_spot')){
                DB::update('spot')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_outdoor')){
                DB::update('outdoor')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_food')){
                DB::update('food')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_tuan')){
                DB::update('tuan')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_techan')){
                DB::update('techan')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
            if(St_Functions::is_normal_app_install('system_malls')){
                DB::update('malls')->set(array('fenxiao_status'=>$fenxiao_status_batch))->execute();
            }
        }

        echo json_encode(array('status'=>true,'msg'=>'保存成功'));
    }

    //分销方式保存
    public function action_ajax_ways_save()
    {
        //$cfg_fenxiao_ways = $_POST['cfg_fenxiao_ways'];
        $ways = array();
        foreach($_POST as $key=>$val)
        {
            if(strpos($key,'way_')===0 && $val==1)
            {
                $way = str_replace('way_','',$key);
                $ways[] = $way;
            }
        }


        $cfg_fenxiao_pic_parts = $_POST['cfg_fenxiao_pic_parts'];

        $_POST['cfg_fenxiao_ways'] = implode(',',$ways);
        $_POST['cfg_fenxiao_pic_parts'] = implode(',',$cfg_fenxiao_pic_parts);
        foreach($_POST as $k=>$v)
        {
            if(strpos($k,'cfg_')!==false)
            {
                $model=ORM::factory('fenxiao_config')->where('varname','=',$k)->find();
                $model->varname=$k;
                $model->value=$v;
                $model->save();
            }
        }
        echo json_encode(array('status'=>true,'msg'=>'保存成功'));
    }

    //分销配置添加
    public function action_configuration()
    {
        $fx_code_logo=Model_Sysconfig::get_configs(0, 'cfg_fx_code_logo', true);
        $fx_code_background=Model_Sysconfig::get_configs(0, 'cfg_fx_code_background', true);
        $info['fx_code_logo']=$fx_code_logo;
        $info['fx_code_background']=$fx_code_background;
        $this->assign('info',$info);
        $this->display('admin/fenxiao/config/configuration');
    }
    //配置提交
    public function action_ajax_configuration()
    {
        $data=$_POST;
        $cfg_arr = array('webid'=>0);
        if(!empty($data['fx_code_logo']))
        {
            $cfg_arr['cfg_fx_code_logo']=$data['fx_code_logo'];
        }
        if(!empty($data['fx_code_background']))
        {
            $cfg_arr['cfg_fx_code_background']=$data['fx_code_background'];
        }
        $sys_model = new Model_Sysconfig();
        $result=$sys_model->save_config($cfg_arr);
        if($result){
            echo json_encode(array('status'=>true,'msg'=>'保存成功'));
        }else{
            echo json_encode(array('status'=>false,'msg'=>'保存失败'));
        }

    }

}